package com.tt.service;

import com.tt.common.page.QueryInfo;
import com.tt.common.page.TtResult;
import com.tt.common.util.IDUtils;
import com.tt.common.util.StringUtils;
import com.tt.mapper.ItemDescMapper;
import com.tt.mapper.ItemMapper;
import com.tt.mapper.ItemParamItemMapper;
import com.tt.pojo.Item;
import com.tt.pojo.ItemDesc;
import com.tt.pojo.ItemParamItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 青青
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ItemServiceImpl implements ItemService
{
    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private ItemDescMapper itemDescMapper;

    @Autowired
    private ItemParamItemMapper paramItemMapper;

    @Override
    public Item getItemById(Long itemId)
    {
        Item itemById = itemMapper.getItemById(itemId);
        ItemDesc itemDesc = itemDescMapper.getItemDesc(itemId);
        itemById.setItemDesc(itemDesc.getItemDesc());
        return itemById;
    }

    @Override
    public TtResult getItemList(QueryInfo queryInfo)
    {
        List<Item> itemList = itemMapper.getItemList(queryInfo);
        Integer itemCount = itemMapper.getItemCount();
        TtResult result = TtResult.ok(itemList);
        result.setTotal(itemCount);
        return result;
    }

    @Override
    public Integer addItem(Item item)
    {
        item.setId(IDUtils.genItemId());
        Integer integer = itemMapper.addItem(item);

        //补全商品属性
        ItemDesc desc = new ItemDesc();
        desc.setItemDesc(item.getItemDesc());
        desc.setItemId(item.getId());
        itemDescMapper.addItemDesc(desc);

        //补全规格参数
        ItemParamItem paramItem = new ItemParamItem();
        paramItem.setItemId(item.getId());
        paramItem.setParamData(item.getParamData());
        paramItemMapper.addItemParam(paramItem);

        return integer;
    }

    @Override
    public Integer delItem(List<Long> ids)
    {
        //删除商品信息
        Integer integer = itemMapper.delItem(ids);

        //商品商品描述
        itemDescMapper.delItemDesc(ids);

        //删除规格参数
        paramItemMapper.delItemParam(ids);

        return integer;
    }

    @Override
    public Integer updateItem(Item item)
    {
        //修改商品信息
        Integer integer = itemMapper.updateItem(item);
        ItemDesc desc = new ItemDesc();
        desc.setItemId(item.getId());
        desc.setItemDesc(item.getItemDesc());

        //修改商品描述
        itemDescMapper.updateItemDesc(desc);

        //修改规格参数
        ItemParamItem paramItem = new ItemParamItem();
        paramItem.setParamData(item.getParamData());
        paramItem.setItemId(item.getId());
        ItemParamItem itemParamItem = paramItemMapper.getItemParamItem(item.getId());
        if (StringUtils.isNull(itemParamItem)){
            return  paramItemMapper.addItemParam(paramItem);
        }
        paramItemMapper.updateItemParam(paramItem);
        return integer;
    }
}
